// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.cmds;

import java.util.Vector;

import tc.common.registry.RegistrySys;

import tc.crase.db.Xattr;
import tc.crase.db.Xobject;
import tc.crase.sys.XobjectEx;
import tc.crase.sys.Xpair;

public class Selection {
	public static boolean objectsAllowed = false;
	public static boolean attrsAllowed   = false;
	
	public static Vector<Object> selset = new Vector<Object>();
	
	public static void reset() {
		objectsAllowed = false;
		attrsAllowed   = false;
	}
	
	public static void add(XobjectEx xobj) {
		xobj.select(true);
		selset.add(xobj);
	}
	
	public static void add(Xattr attr, XobjectEx xobj) {
		if (XobjectEx.isAttrSeleced(attr)) return;
		XobjectEx.attrsSelected.add(new Xpair(xobj, attr));
		selset.add(new Xpair(xobj, attr));
		
		xobj.objex.objChanged();
		RegistrySys.registry.lock();
		RegistrySys.registry.unlock();
	}
	
	public static void clean() {
		for (int i = 0; i < selset.size(); i++) {
			Object object = selset.get(i);
			
			if ( object instanceof XobjectEx) {
				XobjectEx xobj = (XobjectEx)object;
				xobj.select(false);
			}
		}
		
		selset.clear();
		
		for (int i = 0; i < XobjectEx.attrsSelected.size(); i++) {
			Xobject xobj = XobjectEx.attrsSelected.get(i).object;
			if ( xobj instanceof XobjectEx == false) continue;
			XobjectEx xobjEx = (XobjectEx)xobj; 
			xobjEx.objex.objChanged();
		}
		
		RegistrySys.registry.lock();
		RegistrySys.registry.unlock();
	}
}
